package com.telkomsigma.hibernate.persistence.captain.dao;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.telkomsigma.hibernate.common.PaginatedResult;
import com.telkomsigma.hibernate.persistence.captain.entity.Captain;


@Repository
public class CaptainDaoImpl extends HibernateDaoSupport implements CaptainDao {
	
	private final Class<Captain> persistentClass = Captain.class;
	private static final Log log = LogFactory.getLog(CaptainDaoImpl.class);
	
	
	@Autowired
	public CaptainDaoImpl(SessionFactory sessionFactory) {
		setSessionFactory(sessionFactory);
	}
	
	public Class<Captain> getPersistentClass() {
		return persistentClass;
	}


	@Override
	public Captain insert(Captain transientInstance) {
		log.debug("inserting " + this.persistentClass.getName() + " instance");
		try {
			transientInstance.setPlayer_id((String) getHibernateTemplate().save(
					transientInstance));
			log.debug("insert successful");
			return transientInstance;
		} catch (RuntimeException re) {
			log.error("insert failed", re);
			throw re;
		}
	}

	@Override
	public Captain update(Captain detachedInstance) {
		log.debug("updating " + this.persistentClass.getName() + " instance");
		try {
			Captain result = getHibernateTemplate().merge(detachedInstance);
			log.debug("update successful");
			return result;
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}

	@Override
	public Boolean remove(Captain persistentInstance) {
		log.debug("deleting " + this.persistentClass.getName() + " instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
			return true;
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	@Override
	public Boolean removeList(List<Captain> list) {
		log.debug("Removing a list of " + this.persistentClass.getName());
		int count = 0;
		try {
			for (Captain entity : list) {
				getHibernateTemplate().delete(entity);
				count++;
			}
			log.debug("Removing list of " + this.persistentClass.getName()
					+ " succeeded");
			return true;
		} catch (RuntimeException re) {
			log.error("Delete list failed " + count + " row has been deleted.",
					re);
			throw re;
		}
	}

	@Override
	public Captain findById(String id) {
		log.debug("getting " + this.persistentClass.getName()
				+ " instance with id: " + id);
		try {
			Captain instance = (Captain) getHibernateTemplate().get(
					this.persistentClass.getName(), id);
			return instance;
		} catch (RuntimeException re) {
			log.error("find " + this.persistentClass.getName()
					+ " by id failed", re);
			throw re;
		}
	}

	@Override
	public List<Captain> findByExample(Captain instance,
			int... rowStartIdxAndCount) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Captain> findByProperty(String propertyName, Object value,
			int... rowStartIdxAndCount) {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Captain> findAll(int... rowStartIdxAndCount) {
		log.debug("finding all " + this.persistentClass.getName()
				+ " instances");
		try {
			String sqlString = "select model from "
					+ this.persistentClass.getName() + " as model";
			Query query = getHibernateTemplate().getSessionFactory()
					.getCurrentSession().createQuery(sqlString);
			if (rowStartIdxAndCount != null && rowStartIdxAndCount.length > 0) {
				int rowStartIdx = Math.max(0, rowStartIdxAndCount[0]);
				if (rowStartIdx > 0) {
					query.setFirstResult(rowStartIdx);
				}

				if (rowStartIdxAndCount.length > 1) {
					int rowCount = Math.max(0, rowStartIdxAndCount[1]);
					if (rowCount > 0) {
						query.setMaxResults(rowCount);
					}
				}
			}
			return query.list();
		} catch (RuntimeException re) {
			log.error("find all " + this.persistentClass.getName() + " failed",
					re);
			throw re;
		}
	}

	@Override
	public List<Captain> findLikeProperty(String propertyName, Object value,
			int... rowStartIdxAndCount) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Captain> findLikeMapOfProperties(
			Map<String, Object> propertiesMap, int... rowStartIdxAndCount) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Captain> findByMapOfProperties(
			Map<String, Object> propertiesMap, int... rowStartIdxAndCount) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer countAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer countLikeProperty(String propertyName, Object value) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer countByProperty(String propertyName, Object value) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer countLikeMapOfProperties(Map<String, Object> propertiesMap) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer countByMapOfProperties(Map<String, Object> propertiesMap) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PaginatedResult<Captain> paginateLikeMapOfProperties(
			Map<String, Object> propertiesMap, int... rowStartIdxAndCount) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PaginatedResult<Captain> paginateAll(int... rowStartIdxAndCount) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PaginatedResult<Captain> paginateByMapOfProperties(
			Map<String, Object> propertiesMap, int... rowStartIdxAndCount) {
		// TODO Auto-generated method stub
		return null;
	}

}
